// 验证码生成工具
export  default class VerifyCode{
  // 验证码映射表
  static codeMap = {
    number:'0123456789',
    lowercase:'abcdefghijklmnopqrstuvwxyz',
    uppercase:'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  };
  // 每次从映射表三种类型中随机各取一位 组成一个随机组
  static createRandomCodeGroup(){
    let codeGroup = ''
    for (const codeType of Object.values(this.codeMap)) {
      let index = Math.floor(Math.random() * codeType.length)
      codeGroup += codeType[index]
    }
    return codeGroup
  }
  // 每次从随机组中抽取一位 共抽取 n 位组成验证码
  static createVerifiedCode(len = 5){
    let code = ''
    let oldIndex = -1
    let newIndex = 0,
        codeGroup = this.createRandomCodeGroup()

    for (let i = 0; i < len; i++) {
      // 加个过滤条件 防止所有验证码都是同一类型
      if(oldIndex === newIndex){
        newIndex = Math.floor(Math.random() * codeGroup.length)
      }
      code += codeGroup[newIndex]
      codeGroup = this.createRandomCodeGroup()
      oldIndex = newIndex
      newIndex = Math.floor(Math.random() * codeGroup.length)
    }

    return code
  }
}
